package api

import (
	"Wechat/Tools"
	"Wechat/domain"
	"Wechat/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 声明一个service参数用于调用函数
var sourceService service.SourceService

// AddSource 添加资源
func AddSource(c *gin.Context) {
	//	获取页面数据：json
	var sourceMap = make(map[string]interface{})
	c.BindJSON(&sourceMap)
	source := domain.Source{
		Title:    sourceMap["title"].(string),
		Detail:   sourceMap["detail"].(string),
		FilePath: sourceMap["filepath"].(string),
		UnionID:  sourceMap["unionid"].(string),
		Category: sourceMap["category"].(string),
	}
	if source.UnionID == "" {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "unionid is invalid!",
		})
		return
	}
	if source.FilePath == "" {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "filepath is null!",
		})
		return
	}
	//	放入数据
	if sourceService.AddSourceService(source) {
		c.JSON(http.StatusOK, gin.H{
			"msg": http.StatusText(http.StatusOK),
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Add source Fail!",
		})
	}
}

// 用于上传文件并返回路径
func UploadSourceFile(c *gin.Context) {
	//	获取图片并且存储下来
	File, err := c.FormFile("filename")
	Tools.CherrError(err, "Upload source file ERROR!")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Get upload source file Fail!",
		})
		return
	}
	FilePath := Tools.PicturePath + "Source/" + File.Filename
	err = c.SaveUploadedFile(File, FilePath)
	Tools.CherrError(err, "Save source file ERROR!")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Save source file Fail!",
		})
		return
	}
	//	返回文件存储信息
	c.JSON(http.StatusOK, gin.H{
		"msg":      http.StatusText(http.StatusOK),
		"filepath": "http://" + Tools.IPAddr + Tools.Port + "/Picture/Source/" + File.Filename,
	})
}

// GetSourceList 资源列表（根据列表）
func GetSourceList(c *gin.Context) {
	// 获取页面数据：json
	var sourceMap = make(map[string]interface{})
	c.BindJSON(&sourceMap)
	sourceID, err := strconv.Atoi(sourceMap["id"].(string))
	Tools.CherrError(err, "Get source list ERROR!")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "id is wrong!",
		})
		return
	}
	if sourceID <= 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "id is invalid!",
		})
	}
	sourceCategory := sourceMap["category"].(string)
	//	 根据category的不同情况调用不同的函数，返回不同的列表
	if sourceCategory == "" {
		if isOk, sources := sourceService.GetSourceList(sourceID); isOk {
			c.JSON(http.StatusOK, gin.H{
				"msg":  http.StatusText(http.StatusOK),
				"data": sources,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"msg":   "Fail",
				"ERROR": "Get source list Fail!",
			})
		}
	} else {
		if isOk, sources := sourceService.GetSourceCategory(sourceCategory, sourceID); isOk {
			c.JSON(http.StatusOK, gin.H{
				"msg":  http.StatusText(http.StatusOK),
				"data": sources,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"msg":   "Fail",
				"ERROR": "Get source list by category Fail!",
			})
		}
	}
}

// GetSourceDetail 查看资源详情
func GetSourceDetail(c *gin.Context) {
	// 获取页面数据：json
	var sourceMap = make(map[string]interface{})
	c.BindJSON(&sourceMap)
	sourceID, err := strconv.Atoi(sourceMap["id"].(string))
	Tools.CherrError(err, "Get source detail ERROR!")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "id is wrong!",
		})
		return
	}
	if sourceID <= 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "id is invalid!",
		})
		return
	}
	//	调用函数获取详情
	if isOk, source := sourceService.GetSourceDetail(sourceID); isOk {
		c.JSON(http.StatusOK, gin.H{
			"msg":  http.StatusText(http.StatusOK),
			"data": source,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Get detail Fail!",
		})
		return
	}
}
